<?php
// 应用公共文件
/**
 * 成功返回信息
 * status：状态码
 * msg：提示信息
 * data：返回数据
 */
function successMsg($status = 1000, $msg = 'success', $data = []): \think\response\Json
{
    return json([
        'code' => $status,
        'msg' => $msg,
        'data' => $data
    ], 200);
}

/**
 * 失败返回信息
 * status：状态码
 * msg：错误信息
 * data：返回数据
 */
function errMsg($status = 2000, $msg = 'fail', $data = []): \think\response\Json
{
    return json([
        'code' => $status,
        'msg' => $msg,
        'data' => $data
    ], 200);
}

/**
 * @param $list
 * @param $total
 * @param $params
 * @return array
 */
function makePaginate($list , $total , $params): array
{
    return  [
        'total' => $total,
        'pageNum' => $params['pageNum'],
        'pageSize' => $params['pageSize'],
        'list' => $list
    ];
}

function getTree($array, $pid = 0): array
{
    $tree = array();
    foreach ($array as $key => $value) {
        if ($value['pid'] == $pid) {
            $value['children'] = getTree($array, $value['id']);
            $tree[] = $value;
        }
    }
    return multilevelSort($tree);
}

function multilevelSort(array $source, $sort = SORT_DESC, $sortKey = 'sort', $children = 'children'): array
{
    array_multisort(array_column($source, $sortKey), $sort, $source);
    foreach ($source as &$value) {
        if (isset($value[$children])) {
            $value[$children] = multilevelSort($value[$children], $sort, $sortKey, $children);
        }
    }
    return $source;
}
